<template>
	<view class="pl25 pr25" style="background-color: #FFFFFF;">
		<view class="pt30 pb30 flex flex-ac bb">
			<text class="text">收货人</text>
			<input type="text" class="it1" placeholder="请填写收货人姓名" v-model="name"/>
		</view>
		<view class="pt30 pb30 flex flex-ac bb">
			<text class="text">手机号码</text>
			<input type="number" class="it1" placeholder="请填写收货人手机号码" v-model="phone"/>
		</view>
		<view class="pt30 pb30 flex flex-ac bb">
			<text class="text">地址</text>
			<input type="text" class="it1" placeholder="请填写详细地址"  v-model="address"/>
			<view @click="addressImage">
				<image class="address" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/address2.png"></image>
			</view>
		</view>
		<!-- <view class="pt30 pb30 flex flex-ac bb">
			<text class="text">标签</text>
			<text class="type mr30">家</text>
			<text class="type mr30">公司</text>
			<text class="type mr30">学次</text>
			<text class="type">手动输入</text>
		</view> -->
		<view class="pt30 pb30 flex flex-ac">
			<view class="it1">
				<view>设为默认地址</view>
				<view class="ft12 c74a6fd">默认为每次配送地址</view>
			</view>
			<switch color="#74a6fd" :checked="switch1Checked" @change="switch1Change"/>
		</view>
		<!-- <view class="bottomBtn ftc">
			<view class="bottomBtncon bgfff"><text class="cfff btns ct ftc block" @click="save">保存</text></view>
		</view> -->
		<view style="width: 100vw;position: fixed;bottom: 40rpx;display: flex;align-items: center;justify-content: space-around;padding: 0rpx;left: 0;">
			<view class="add-btn"  @click="save">保存</view>
		</view>
		<uni-popup ref="popup" type="message">
		    <uni-popup-message :type="warnType" :message=warnMessage :duration="warnTime"></uni-popup-message>
		</uni-popup>
		
		<view style="position: fixed;bottom: 140rpx;right: 50rpx;
			width: 120rpx;height: 120rpx;background: linear-gradient(0deg, #00BAAD, #81DCD6);
			box-shadow: 0px 0px 22rpx 2rpx rgba(162, 215, 211, 0.91);border-radius: 50%;
			display: flex;flex-direction: column;align-items: center;justify-content: center;z-index: 999;"
			@click="tohome">
			<image src="../../static/images/sy@2x.png" style="width: 50%;" mode="widthFix"></image>
			<view style="color: #FFFFFF;">首页</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				switch1Checked: false,
				name:'',//姓名
				phone:'',//电话
				isdefault:0,//是否默认地址
				longitude:0,//经度
				latitude:0,//纬度
				address:'',//详细地址
				warnMessage:'错误',//消息内容
				warnType:'warn',//消息类型  success/warn/info/error
				warnTime:1000,//消息提醒延长时间
				usrUserId:'',//用户表id
				addressId:0,//地址id
			};
		},
		onLoad(option) {
			var that = this;
			this.usrUserId = option.userId;
			if(option.addressId){
				uni.setNavigationBarTitle({
				   title: '修改收货地址'
				})
				that.addressId = option.addressId;
				that.func.$request('applyRecipe/selectAddressInfo.tz',{id : that.addressId },'GET').then(res=>{
							if(res.data.success){
								if(res.data.result.name){
									that.name = res.data.result.name;
								}
								if(res.data.result.phone){
									that.phone = res.data.result.phone;
								}
								if(res.data.result.isdefault){
									that.isdefault = res.data.result.isdefault;
								}
								if(res.data.result.longitude){
									that.longitude = res.data.result.longitude;
								}
								if(res.data.result.latitude){
									that.latitude = res.data.result.latitude;
								}
								if(res.data.result.address){
									that.address = res.data.result.address;
								}
								if(that.isdefault == 1){
									that.switch1Checked = true;
								}
							}
							
						})
					}
				},
		onShow() {},
		methods: {
			tohome(){
				uni.reLaunch({
					url: '/pages/index/index'
				})
			},
			switch1Change(e) {
				if(e.target.value ==false){
					this.isdefault = 0;
				}else{
					this.isdefault = 1;
				}
			},
			//保存
			save(){
				var that = this;
				if(that.verification()){
					var parm = {};
					parm.name=that.name;
					parm.phone=that.phone;
					parm.isdefault=that.isdefault;
					parm.usrUserId=that.usrUserId;
					parm.longitude = that.longitude;
					parm.latitude = that.latitude;
					parm.address = that.address;
					if(that.addressId!=0){
						parm.id =that.addressId;
					}
					console.log(parm)
					let openid=uni.getStorageSync('openId')
					uni.showModal({
					    title: '提示',
					    content: '是否确定提交信息',
					    success:  (res) =>{
					        if (res.confirm) {
					           that.func.$request('applyRecipe/addReceiveAddress.tz',parm,'POST','application/x-www-form-urlencoded').then(item=>{
					           						if(item.data.success){
					           							that.warnMessage = '保存成功';
					           							that.warnType = 'success';
					           							that.$refs.popup.open();
					           							setTimeout(()=>{
					           								//地址列表的跳转
					           								// uni.navigateTo({
					           								// 	url: '/pages/address/index?userId='+that.usrUserId
					           								// });
					           								let pages = getCurrentPages();
					           								let prevPage = pages[pages.length-2]
					           								prevPage.$vm.isBack = true
					           								uni.navigateBack({
					           									delta:1
					           								})
					           							},1000)
					           						}else{
					           							that.warnMessage = item.data.errMsg;
					           							that.warnType = 'error';
					           							that.warnTime  = 3000;
					           							that.$refs.popup.open()
					           						}
					           						
					           					}).catch(error => {
					           						that.warnMessage = '新增地址失败'
					           						that.warnType = 'error'
					           						that.$refs.popup.open()
					           						});
					           		        } else if (res.cancel) {
					           		            
					           		        }
					           		    }
					           		});
					           	};
					           },
			//数据验证
			verification(){
				 if(!this.name){
				 	this.warnMessage = '收货人姓名不能为空';
				 	this.$refs.popup.open();
				 	return false;
				 }
				 if(!this.phone){
				 	this.warnMessage = '电话号码不能为空';
				 	this.$refs.popup.open();
				 	return false;
				 }
				 if(!this.address){
				 	this.warnMessage = '地址不能为空';
				 	this.$refs.popup.open();
				 	return false;
				 }
				
				return true;
			},
			addressImage(){
				var that = this;
				uni.chooseLocation({
				    success: function (res) {
						that.longitude = res.longitude;
						that.latitude = res.latitude;
						that.address = res.address;
				    }
				});
			}
		}
	};
</script>

<style lang="less">
	.add-btn{
		margin: auto;
		height: 88rpx;
		line-height: 88rpx;
		width: 90%;
		background:  #BE965B;
		border-radius: 44rpx;
		color: #FFFFFF;
		text-align: center;
	}
	.text{
		width: 160rpx;
		height: 1.4rem;
		line-height: 1.4rem;
	}
	.address{
		width: 47rpx;
		height: 48rpx;
		margin-right: 28rpx;
	}
	.type{
		display: inline-block;
		line-height: 50rpx;
		border-radius: 5rpx;
		border: 1rpx solid #666;
		padding: 0 20rpx;
	}
	switch{
	  transform: scale(0.8)
	}
	.bottomBtn {
		position: relative;
		height: 125rpx;
		width: 100%;
		.bottomBtncon {
			position: fixed;
			width: 100%;
			z-index: 10;
			left: 0;
			height: 125rpx;
			bottom: 0;
			background: #ffffff;
			.btns {
				width: 660rpx;
				height: 100rpx;
				line-height: 100rpx;
				border-radius: 10rpx;
				background: #74a6fd;
			}
		}
	}
</style>
